require "prefabutil"

local assets =
{
    Asset("ANIM", "anim/tf_pond.zip"),
    Asset("ANIM", "anim/splash.zip"),
}

local prefabs_crab_pond =
{
    "tf_crab",
    "collapse_big",
}

local prefabs_shrimp_pond =
{
    "tf_green_shrimp",
    "collapse_big",
}

local function onhammered(inst, worker)
    inst.components.lootdropper:DropLoot()
    local fx = SpawnPrefab("collapse_big")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("metal")
    inst:Remove()
end

local function OnSave(inst, data)
end

local function OnLoad(inst, data)
end

local function commonfn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    MakePondPhysics(inst, 1.95)

    inst.AnimState:SetBuild("tf_pond")
    inst.AnimState:SetBank("tf_pond")
    inst.AnimState:PlayAnimation("idle", true)
    inst.AnimState:SetOrientation(ANIM_ORIENTATION.OnGround) --贴地动画
    inst.AnimState:SetLayer(LAYER_BACKGROUND)
    inst.AnimState:SetSortOrder(3)                           --较低的绘制优先级

    inst.MiniMapEntity:SetIcon("tf_crab_pond.tex")

    inst:AddTag("watersource")              --水源
    inst:AddTag("pond")
    inst:AddTag("antlion_sinkhole_blocker") --不受蚁狮地震影响
    inst:AddTag("birdblocker")              --鸟不会停留在池塘上

    inst.no_wet_prefix = true               --没有潮湿的前缀

    inst:SetDeploySmartRadius(2)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst:AddComponent("fishable")
    inst.components.fishable:SetRespawnTime(TUNING.FISH_RESPAWN_TIME)

    inst:AddComponent("lootdropper")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(4)
    inst.components.workable:SetOnFinishCallback(onhammered)

    inst:AddComponent("hauntable")
    inst.components.hauntable:SetHauntValue(TUNING.HAUNT_TINY)

    inst:AddComponent("watersource")

    inst.OnSave = OnSave
    inst.OnLoad = OnLoad

    return inst
end

local function pondcrab()
    local inst = commonfn()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.components.fishable:AddFish("tf_crab")

    return inst
end

local function pondshrimp()
    local inst = commonfn()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.components.fishable:AddFish("tf_green_shrimp")

    return inst
end

return
    Prefab("tf_crab_pond", pondcrab, assets, prefabs_crab_pond),
    MakePlacer("tf_crab_pond_placer", "tf_pond", "tf_pond", "idle", true),
    Prefab("tf_green_shrimp_pond", pondshrimp, assets, prefabs_shrimp_pond),
    MakePlacer("tf_green_shrimp_pond_placer", "tf_pond", "tf_pond", "idle", true)
